package com.carlinfo.userscenter.users.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.carlinfo.usercenter.common.dao.impl.BaseDaoImpl;
import com.carlinfo.usercenter.common.util.PageInfoUtil;
import com.carlinfo.userscenter.users.dao.IAAdminsDao;
import com.carlinfo.userscenter.users.pojo.AAdmins;

public class AAdminsDaoImpl extends BaseDaoImpl<AAdmins> implements IAAdminsDao
{
	
	@Override
	public int saveDao(AAdmins admins)
	{
		int res = 0 ;
		System.out.println("==IAAdminsDaoImpl==");
		/* 添加sql语句 */
		
		String sql = "insert into `a_admins`(`roleId`, `email`, `password`, `photoPath`, `trueName`, "
				+ "`phone`, `qq`, `loginFailedCount`, `loginFailedTime`, `loginCount`, `lastLoginIp`, "
				+ "`status`, `createTime`, `updateTime`, `pubTime`, `lastloginTime`,`sex`) "
				+ "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)";
		List<Object> paramsList = new ArrayList();
		paramsList.add(admins.getRoleId());
		paramsList.add(admins.getEmail());
		paramsList.add(admins.getPassword());
		paramsList.add(admins.getPhotoPath());
		paramsList.add(admins.getTrueName());
		paramsList.add(admins.getPhone());
		paramsList.add(admins.getQq());
		paramsList.add(admins.getLoginFailedCount());
		paramsList.add(admins.getLoginFailedTime());
		paramsList.add(admins.getLoginCount());
		paramsList.add(admins.getLastLoginIp());
		paramsList.add(admins.getStatus());
		paramsList.add(admins.getCreateTime());
		paramsList.add(admins.getUpdateTime());
		paramsList.add(admins.getPubTime());
		paramsList.add(admins.getLastloginTime());
		paramsList.add(admins.getSex());
		
		res = this.executeSqlUpdate(sql, paramsList);
		/**
		 * 主键被放到了list中
		 * 如何取到最后一个值呢
		 */
		admins.setId((int) paramsList.get(paramsList.size() - 1 ));
		return res ; 
	}

	@Override
	public int updateDao(AAdmins admins)
	{
		/* sql语句 */
		String sql = "update `a_admins` set `roleId`  =? , `email` =? , `password` =? , `photoPath` =? , `trueName` =? , `phone` =? , `qq` =? , `loginFailedCount` =? , `loginFailedTime` =? , "
				+ "`loginCount` =? , `lastLoginIp` =? , `status` =? , "
				+ "`createTime` =? , `updateTime` =? , `pubTime` =? , `lastloginTime` = ?,"
				+ "`sex` = ? "
				+ " where `id` = ?" ;
		List<Object> paramsList = new ArrayList<Object>();
		paramsList.add(admins.getRoleId());
		paramsList.add(admins.getEmail());
		paramsList.add(admins.getPassword());
		paramsList.add(admins.getPhotoPath());
		paramsList.add(admins.getTrueName());
		paramsList.add(admins.getPhone());
		paramsList.add(admins.getQq());
		paramsList.add(admins.getLoginFailedCount());
		paramsList.add(admins.getLoginFailedTime());
		paramsList.add(admins.getLoginCount());
		paramsList.add(admins.getLastLoginIp());
		paramsList.add(admins.getStatus());
		paramsList.add(admins.getCreateTime());
		paramsList.add(admins.getUpdateTime());
		paramsList.add(admins.getPubTime());
		paramsList.add(admins.getLastloginTime());
		paramsList.add(admins.getSex());
		paramsList.add(admins.getId());
		return this.executeSqlUpdate(sql, paramsList) ; 
	}
	
	/**
	 * 
	 */
	@Override
	public int deleteDao(AAdmins admins)
	{
		/*------只关心SQL语句和条件-------*/
		String sql = "delete from a_admins "
				+ " where `id` = ? " ; ;
		/* 存储数据 */
		List<Object> paramsList = new ArrayList<Object>();
		paramsList.add(admins.getId());
		return this.executeSqlUpdate(sql, paramsList) ; 
	}

	@Override
	public AAdmins findDao(Map<String, Object> condMap)
	{
		/*-----------拼装sql语句和条件-----------*/
		/**
		 * 存储条件
		 * condMap:用户输入的搜索条件
		 * paramsList;存储的是sql语句和条件
		 */
		List<Object> paramsList =  new ArrayList<Object>();
		/* sql语句和条件
		 * ？的位置和list的顺序一定要一致
		 * */
		String sql = "select * from a_admins " ;
		/* 根据id来查询 */
		if(condMap.get("id") != null && !"".equalsIgnoreCase(condMap.get("id") + ""))
		{
			/* 您已经传了id */
			sql += "where id = ? " ;
			paramsList.add(condMap.get("id"));
		}else if(condMap.get("email") != null && !"".equalsIgnoreCase(condMap.get("email") + ""))
		{
			/* 您已经传了name */
			sql += "where email = ? " ;
			paramsList.add(condMap.get("email"));
		}
		System.out.println("===AAdmins==findOne===sql==" + sql) ;
		System.out.println("===AAdmins==findOne===条件===" + paramsList);
		
		return this.findOneBase(sql, paramsList, new AAdmins());
	}

	/**
	 * 分页:
	 * 原始的SQL语句: select * from a_admins ; 
	 * ~获取总记录数: select count(*) from a_admins ; 
	 * ~分页:   select * from a_admins limit 1(currentRecord),5(pageSize);(参数1:是从第几条开始(从0开始的),参数2:往后查询多少条)
	 * 含有搜索的sql语句+分页:
	 * ~原始的SQL语句:select * from a_admins where (name like '%2%' or content like '%2%') and status = 1 and pubTime > '2020-11-11 00:00:00' and pubTime < '2020-12-11 00:00:00' ;
	 * ~获取总记录数: select count(*) from a_admins where (name like '%2%' or content like '%2%') and status = 1 and pubTime > '2020-11-11 00:00:00' and pubTime < '2020-12-11 00:00:00' ;
	 * ~分页数据:    select * from a_admins where (name like '%2%' or content like '%2%') and status = 1 and pubTime > '2020-11-11 00:00:00' and pubTime < '2020-12-11 00:00:00' limit 1 , 1 ;
	 	三个条件任意组合;
	 	
	 	~关键字,状态,时间范围
	 	select * from a_admins where (name like '%2%' or content like '%2%') and status = 1 and pubTime > '2020-11-11 00:00:00' and pubTime < '2020-12-11 00:00:00' ;
	 	select * from a_admins where (name like '%2%' or content like '%2%');
	 	select * from a_admins where status = 1;
	 	select * from a_admins where pubTime > '2020-11-11 00:00:00' and pubTime < '2020-12-11 00:00:00';
	 	select * from a_admins where (name like '%2%' or content like '%2%') and status = 1;
	 	select * from a_admins where status = 1 and pubTime > '2020-11-11 00:00:00' and pubTime < '2020-12-11 00:00:00';
	 	select * from a_admins where (name like '%2%' or content like '%2%') and pubTime > '2020-11-11 00:00:00' and pubTime < '2020-12-11 00:00:00';
	 
	 	要实现排序:
	 	select * from a_admins where (name like '%2%' or content like '%2%') and status = 1 and pubTime > '2020-11-11 00:00:00' and pubTime < '2020-12-11 00:00:00' 
	 	order by id desc , name asc limit 1 ,1 ; 
	 	计算总条数的SQL语句:
	 	select count(*) from a_admins where (name like '%2%' or content like '%2%') and status = 1 and pubTime > '2020-11-11 00:00:00' and pubTime < '2020-12-11 00:00:00' 
	 	; 
	 */
	@Override
	public List<AAdmins> findCountList(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		System.out.println("==AAdminsDaoImpl========findCountList===========");
		/* sql语句的条件 */
		List<Object> paramList = new ArrayList<Object>();
		/* sql语句的条件 */
		/*-----------拼装sql语句和条件-----------*/
		/* sql语句和条件
		 * ？的位置和list的顺序一定要一致
		 * */
		String sql = "select * from a_admins where 1 = 1 " ;
		/*	
		 *	where后面跟的条件,第一个不能加and或者or
		 *解决此难题在where后面加一个永远成立的条件;1=1 
		 */
		/* 关键字 */
		if(condMap.get("keyword") != null & !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			sql = sql + " and ( email like ? or phone like ? or qq like ? ) " ;
			/* 有几个？就放几个条件 */
			paramList.add("%" + condMap.get("keyword") + "%" );
			paramList.add("%" + condMap.get("keyword") + "%");
			paramList.add("%" + condMap.get("keyword") + "%");
		}
		
		/* 状态 */
		if(condMap.get("status") != null && !"".equalsIgnoreCase(condMap.get("status") + ""))
		{
			sql = sql + " and status = ? " ;
			paramList.add(condMap.get("status"));
		}
		
		/* 时间范围 
		 * 从map中取出的值是Object,想判断一下,是不是Date对象
		 * 怎么判断:A是不是B的一个对象呢(A是对象,B是类)
		 * */
		if(condMap.get("stTime") instanceof Date && condMap.get("edTime") instanceof Date)
		{
			sql = sql + " and createTime > ? and createTime = ? " ;
			paramList.add(condMap.get("stTime"));
			paramList.add(condMap.get("edTime"));
		}
		
		/* 默认都有orderby */
		if("iddesc".equalsIgnoreCase(condMap.get("orderby") + ""))
		{
			sql = sql + " order by id desc " ;
		}else if("idasc".equalsIgnoreCase(condMap.get("idasc") + ""))
		{
			sql = sql + " order by id asc " ;
		}else
		{
			sql = sql + " order by id desc " ;
		}
		
		System.out.println("===AAdmins==findOne===sql==" + sql) ;
		
		/**
		 * 分页查询
		 * 不分页查询
		 * 搜索查询
		 */
		if(pageInfoUtil != null)
		{
			/* 分页查询 */
			return this.findByPage(pageInfoUtil, sql, paramList, new AAdmins());
		}
		/* 不分页查询 */
		return this.find(sql, paramList, new AAdmins());

	}
}
